﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Krile.Kernel.WindowsEx;

namespace Wassrile.Dialogs
{
    public partial class CustomPalletEditor : Form
    {

        public string[] PictogramList
        {
            get
            {
                List<string> list = new List<string>();
                foreach (var i in cpList.Items)
                {
                    list.Add(i as string);
                }
                return list.ToArray();
            }
            set
            {
                cpList.Items.Clear();
                if (value != null)
                    cpList.Items.AddRange(value);
            }
        }

        public CustomPalletEditor()
        {
            InitializeComponent();
            updateEnables();
        }

        private void CustomPalletEditor_Shown(object sender, EventArgs e)
        {
            this.Text = "絵文字を読み込んでいます...";
            this.Refresh();
            Application.DoEvents();
            pallet.Pictograms = Core.PictogramResolver.GetCodeCandidates().ToArray<string>();
            this.Refresh();
            this.Text = "カスタムパレットエディタ";
        }

        private void pallet_OnClickPallet(object sender, Wassrile.Dialogs.Pictogram.PictogramSet.StringEventArgs e)
        {
            if (cpList.Items.Contains(e.code))
            {
                MessageBoxEx.Show(
                    e.code + "はすでに存在します。",
                    "同じものは追加できません。",
                    "カスタムパレットの編集", MessageBoxEx.MessageBoxExButtons.Close, MessageBoxEx.MessageBoxExIcon.Warning);
            }
            else
            {
                cpList.Items.Add(e.code);
                updateEnables();
            }
        }

        private void myListList_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index == -1) return;
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                Rectangle dBound = e.Bounds;
                dBound.Y--;
                dBound.Height += 2;
                using (var b = new LinearGradientBrush(dBound, Color.DodgerBlue, Color.RoyalBlue, 90F))
                {
                    e.Graphics.FillRectangle(b, e.Bounds);
                }
            }
            else
            {
                if (e.Index % 2 == 0)
                {
                    e.Graphics.FillRectangle(Brushes.White, e.Bounds);
                }
                else
                {
                    e.Graphics.FillRectangle(Brushes.Gainsboro, e.Bounds);
                }
            }
            var idata = Core.PictogramResolver.GetPictogram(cpList.Items[e.Index] as string);
            e.Graphics.DrawImage(idata, new Point(e.Bounds.Left + 1, e.Bounds.Top + 1));
            TextRenderer.DrawText(
                e.Graphics,
                cpList.Items[e.Index] as string,
                this.Font,
                new Rectangle(e.Bounds.Left + 2 + idata.Width, e.Bounds.Top, e.Bounds.Width - 2 - idata.Width, e.Bounds.Height),
                e.ForeColor,
                TextFormatFlags.EndEllipsis | TextFormatFlags.Left | TextFormatFlags.NoPrefix | TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter); ;
            e.DrawFocusRectangle();
        }

        private void updateEnables()
        {
            moveUp.Enabled =
                cpList.Items.Count > 0 &&
                cpList.SelectedIndex > 0;
            moveDown.Enabled =
                cpList.Items.Count > 0 &&
                cpList.SelectedIndex >= 0 &&
                cpList.SelectedIndex < cpList.Items.Count - 1;
            itemDelete.Enabled =
                cpList.Items.Count > 0 &&
                cpList.SelectedIndex >= 0 &&
                cpList.SelectedIndex < cpList.Items.Count;
        }

        private void myListList_SelectedIndexChanged(object sender, EventArgs e)
        {
            updateEnables();
        }

        private void moveUp_Click(object sender, EventArgs e)
        {
            int selIdx = cpList.SelectedIndex;
            var item = cpList.Items[selIdx];
            cpList.Items.RemoveAt(selIdx);
            cpList.Items.Insert(--selIdx, item);
            cpList.SelectedIndex = selIdx;
        }

        private void moveDown_Click(object sender, EventArgs e)
        {
            int selIdx = cpList.SelectedIndex;
            var item = cpList.Items[selIdx];
            cpList.Items.RemoveAt(selIdx);
            cpList.Items.Insert(++selIdx, item);
            cpList.SelectedIndex = selIdx;
        }

        private void itemDelete_Click(object sender, EventArgs e)
        {
            cpList.Items.RemoveAt(cpList.SelectedIndex);
        }

    }
}
